package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.appcontact.A003Contact;
import com.example.appcontact.R;
import com.example.base.BaseActivity;
import com.example.model.Contacts;
import com.example.model.Infications;

public class PopupInforAdapter extends ArrayAdapter<Infications>{

	private LayoutInflater inflater;
	private List<Infications> mArr;
	private int layout;
	private OnClickListListener listener;
	private String emailLoged = "";
	private A003Contact context;
	
	public PopupInforAdapter(A003Contact a001, int textViewResourceId,
			List<Infications> objects,String emaillogged) {
		super(a001, textViewResourceId, objects);
		this.emailLoged = emaillogged;
		this.context = a001;
		this.layout = textViewResourceId;
		this.mArr = objects;
		this.inflater = (LayoutInflater) a001.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getViewPanel(position, convertView);
	}
	
	@Override
	public Infications getItem(int position) {
		return super.getItem(position);
	}
	
	@Override
	public int getCount() {
		return mArr.size();
	}
	
	class ViewHolder
    {
    	TextView tvFullName;
    	Button  btCheck,btCancel;
    }
	public static interface OnClickListListener {
        public abstract void onClick(Contacts data);
    }
	private View getViewPanel(int position, View convertView)
	{
		View view = convertView;
        ViewHolder mHolder = null;
        
        if (view == null) {
            view = inflater.inflate(layout, null);
            mHolder            = new ViewHolder();
            mHolder.tvFullName        = (TextView)view.findViewById(R.id.item_popup_info_tv_check);
            mHolder.btCheck           = (Button)view.findViewById(R.id.item_popup_info_bt_check);
            mHolder.btCancel          = (Button) view.findViewById(R.id.item_popup_info_bt_cancel);
            view.setTag(mHolder);
        }
        else
        {
        	mHolder = (ViewHolder) view.getTag();
        }
        if(mArr.size() > 0){
        	final Infications contact  = mArr.get(position);
        	
            	if(contact.getForm().getEmail().equals(emailLoged)){
            			setText(contact.getTo().getEmail(),
                				context.getResources().getString(R.string.popup_list_request_from_logged),
                				mHolder.tvFullName);
            			mHolder.btCancel.setVisibility(View.GONE);
            			mHolder.btCheck.setVisibility(View.GONE);
            	}else {
            		
            		    mHolder.btCancel.setVisibility(View.VISIBLE);
        			    mHolder.btCheck.setVisibility(View.VISIBLE);
        			    
            		        setText(contact.getForm().getEmail(),
            				context.getResources().getString(R.string.popup_list_request_from_user),
            				mHolder.tvFullName);
            		        
            		        mHolder.btCancel.setOnClickListener(new OnClickListener() {
            					@Override
            					public void onClick(View v) {
            						Log.d("CLICK CANNCEL !!!", "Click cancel Request");
            						context.callBackCancel(contact.getId());
            					}
            				});
                			mHolder.btCheck.setOnClickListener(new OnClickListener() {
            					@Override
            					public void onClick(View v) {
            						Log.d("CLICK CHECK !!!", "Click check Request");
            						context.callBackAccept(contact.getId());
            					}
            				});
            		}
        }

        return view;
	}
	private void  setText(String email,String notificatsText,TextView textView){
		Spanned source = Html.fromHtml
				("<font color=\"#3366FF\">" + email + "  </font>" + "<font color=\"#000000\">" +
		                notificatsText + "</font>");
		textView.setText(source);
	}
}